package com.anlogic.sdk.app.ui;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

import com.anlogic.sdk.app.model.IApplicationProject;
import com.anlogic.sdk.bsp.model.IPlatformProject;
import com.anlogic.sdk.bsp.utils.PlatformUtils;

public class ChangeReferenceBspDialog extends TitleAreaDialog  {
	private List bspList;
	
	private IApplicationProject appProj;
	
	private IPlatformProject bspProj;
	
	private IPlatformProject newBspProj;
	
	public ChangeReferenceBspDialog(Shell parentShell, IApplicationProject appProj) {
		super(parentShell);
		setShellStyle(getShellStyle() | SWT.RESIZE);
		this.appProj = appProj;
		this.bspProj = this.appProj.getBspProj();
	}

	private void enableOkButton(boolean flag) {
		Button button = getButton(0);
		if (button != null)
			button.setEnabled(flag);
	}
	
	@Override
	protected Control createButtonBar(Composite parent) {
		Control c = super.createButtonBar(parent);
		validatePage();
		return c;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		setTitle("Change Referenced Platform Project");
		setMessage("Select the platform project to reference");
		
		Composite container = new Composite(parent, SWT.DEFER);
		container.setLayout(new GridLayout(3, false));
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		bspList = new List(container, SWT.SIMPLE);
		bspList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
		
		java.util.List<IPlatformProject> platProjects = PlatformUtils.getPlatformProjectsInWorkspace();
		for (IPlatformProject plat : platProjects) {
			if (plat.getName().equals(bspProj.getName()))
				continue;
			if(!plat.getChip().equals(bspProj.getChip()))
				continue;
			if(!plat.getProcessor().equals(bspProj.getProcessor()))
				continue;
			if (!plat.getOs().equals(bspProj.getOs()))
				continue;
			bspList.add(plat.getName());
		}
		if (!platProjects.isEmpty())
			bspList.select(0);
		
		addListeners();
		
		validatePage();
		
		return (Control) parent;
	}
	
	private void addListeners() {
		bspList.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				validatePage();
			}
		});
	}
	
	private void validatePage() {
		if (!validateBsp()) {
			enableOkButton(false);
			return;
		}
		
		setMessage(null);
		setErrorMessage(null);
		
		enableOkButton(true);
	}
	
	private boolean validateBsp() {
		int index = bspList.getSelectionIndex();
		if (index == -1) {
			setErrorMessage("There is no other applicable platform project in the workspace except the platform project used by the app itself!");
			return false;
		}
		return true;
	}
	
	protected void okPressed() {
		int index = bspList.getSelectionIndex();
		String bspName = bspList.getItem(index);
		newBspProj = PlatformUtils.getPlatformProject(bspName);
		super.okPressed();
	}
	
	public IPlatformProject getNewBsp() {
		return newBspProj;
	}
}
